﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;

namespace converter.template
{
    public enum FontStyle
    {
        Default, // Default is treated as Normal
        Normal,
        Italic,
    }

    public enum FontWeight
    {
        Default, // Default is treated as Normal
        Thin,
        ExtraLight,
        Light,
        Normal,
        Medium,
        SemiBold,
        Bold,
        ExtraBold,
        Heavy,
    }

    // Represents a font attributes of the Style element
    public sealed class Font
    {
        // Fields

        private readonly FontStyle style;
        private readonly string family;
        private readonly Size size;
        private readonly FontWeight weight;

        // Constructors

        // Creates an instance with the default values.
        public Font()
        {
            style = FontStyle.Default;
            family = "Arial";
            size = new Size("10pt");
            weight = FontWeight.Default;
        }

        // Creates an instance with the values read from the style element.
        public Font(XElement style)
            : this()
        {
            Debug.Assert(style != null, "The style argument can not be null.");

            XElement e;

            // Font style
            e = style.Elements().Where(x => x.Name.LocalName == "FontStyle").SingleOrDefault();
            if (e != null)
                this.style = (FontStyle)Enum.Parse(typeof(FontStyle), e.Value, true);

            // Font family
            e = style.Elements().Where(x => x.Name.LocalName == "FontFamily").SingleOrDefault();
            if (e != null)
                family = e.Value;

            // Font size
            e = style.Elements().Where(x => x.Name.LocalName == "FontSize").SingleOrDefault();
            if (e != null)
                size = new Size(e.Value);

            // Font weight
            e = style.Elements().Where(x => x.Name.LocalName == "FontWeight").SingleOrDefault();
            if (e != null)
                weight = (FontWeight)Enum.Parse(typeof(FontWeight), e.Value, true);
        }

        // Accessors

        // Gets a font style.
        public FontStyle Style
        {
            get { return style; }
        }

        // Returns true if the style is italic.
        public bool IsItalic
        {
            get { return style > FontStyle.Normal; }
        }

        // Gets a font family.
        public string Family
        {
            get { return family; }
        }

        // Gets the font's size.
        public Size Size
        {
            get { return size; }
        }

        // Gets the size as a half point measurement. This is what is expected to be put into a Word document.
        public string HalfPointMeasurement
        {
            get { return (size.Value * 2).ToString(); }
        }

        // Gets the font's weight.
        public FontWeight Weight
        {
            get { return weight; }
        }

        // Returns true if the font is bold.
        public bool IsBold
        {
            get { return weight > FontWeight.Normal; }
        }
    }
}
